package com.archstoday.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.utils.ToolUtils;
import com.base.utils.StringUtil;

public class WebActivity extends AsBaseActivity {

    private TextView mTitleTv;
    private WebView mWebView;
    private ProgressBar mProgress;
    private String url;

    private boolean isSuccess;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_web);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mWebView = (WebView) findViewById(R.id.details_content_view);
        mProgress = (ProgressBar) findViewById(R.id.progress_bar);
    }

    @Override
    protected void initData(Bundle bundle) {
        initSetting();
        isSuccess = false;
        url = getIntent().getStringExtra("url");
        mTitleTv.setText(StringUtil.isEmpty(getIntent().getStringExtra("title"))
                ? "原链接" : getIntent().getStringExtra("title"));

        if (ToolUtils.getNetWorkState(mContext) != -1) {
            mWebView.loadUrl(url);
        } else {
            mProgress.setVisibility(View.GONE);
            showToast(R.string.intent_error);
        }
    }

    private void initSetting() {
        WebSettings ws = mWebView.getSettings();
        ws.setBuiltInZoomControls(false);
        ws.setSupportZoom(false);
        ws.setJavaScriptEnabled(true);
        ws.setAllowFileAccess(true);
        ws.setSupportMultipleWindows(true);
        ws.setJavaScriptCanOpenWindowsAutomatically(true);
        ws.setLoadsImagesAutomatically(true);
        ws.setDomStorageEnabled(true);
        ws.setDisplayZoomControls(false);
        ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        ws.setUseWideViewPort(true);
        ws.setLoadWithOverviewMode(true);
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    isSuccess = true;
                    mProgress.setVisibility(View.GONE);
                }
            }
        });
        mWebView.setWebViewClient(new WebViewClient()
//        {
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                isSuccess = true;
//                mProgress.setVisibility(View.GONE);
//            }
//        }
        );
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.back_btn);
    }

    @Override
    public void treatClickEvent(int id) {
        switch (id) {
            case R.id.back_btn:
                appFinish();
                break;
        }
    }

    @Override
    protected void intentState(boolean state) {
        if (state) {
            if (!isSuccess) {
                mProgress.setVisibility(View.VISIBLE);
                mWebView.loadUrl(url);
            }
        } else {
            showToast(R.string.intent_error);
            mProgress.setVisibility(View.GONE);
        }
    }
}